package org.wdcode.base.util;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.opensymphony.xwork2.ActionContext;

/**
 * Struts2 工具类
 * @author WD
 * @since JDK6
 * @version 1.0 2009-09-22
 */
public final class Struts2Util {
	/* struts2 request key */
	private final static String	ACTICON_MAPPING_KEY;

	/**
	 * 静态初始化
	 */
	static {
		ACTICON_MAPPING_KEY = "struts.actionMapping";
	}

	/**
	 * 获得Request
	 * @return Request
	 */
	public final static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得Response
	 * @return Response
	 */
	public final static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得ServletContext
	 * @return ServletContext
	 */
	public final static ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	/**
	 * 获得Session
	 * @return Session
	 */
	public final static HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获得Session
	 * @param b
	 * @return Session
	 */
	public final static HttpSession getSession(boolean b) {
		return getRequest().getSession(b);
	}

	/**
	 * 获得Struts2 提供的Session代替对象,是个Map
	 * @return Map<String, Object>
	 */
	public final static Map<String, Object> getSessionMap() {
		return ActionContext.getContext().getSession();
	}

	/**
	 * 获得ActionMapping
	 * @return ActionMapping
	 */
	public final static ActionMapping getActionMapping() {
		return (ActionMapping) getRequest().getAttribute(ACTICON_MAPPING_KEY);
	}
}
